<?php

//Ajax控制器
class Ajax extends Controller {

	function Ajax()
	{
		parent::Controller();	
	}
	
	//获取广告banner
	function get_banner()
	{
		$adstring = "";
		$this->load->model("ads_model");
		$placeid = p("placeid");
		$ad = $this->ads_model->get($placeid);
		if (is_array($ad) && count($ad)>0) {
			$this->ads_model->setviews($ad['adsid']);
			$adstring = "<a href='".$this->setlink($ad['adsid'], $ad['linkurl'])."' target='_blank'><img width='".$ad['place']['width']."' height='".$ad['place']['height']."' src='".gurl("data/upload_pic").'/'.$ad['imageurl']."'></a>";
		}
		echo $adstring;
		exit;
	}
	
	//设置链接
	function setlink($adsid, $linkurl)
	{
		$linkurl = gurl('/ads/link/?url='.$linkurl.'&adsid='.$adsid);
		return $linkurl;
	}
	
	//标记新鲜事
	function read_fresh()
	{
		$this->load->model("fresh_model");
		$userid = p("userid");
		$this->fresh_model->set_read($userid);
		$msg = "设置成功";
		exit($msg);
	}
	
	//赞美用户
	function set_digg()
	{
		$this->load->model("digg_model");
		$userid = p("userid");
		$author = p("author");
		$dataid = p("dataid");
		$option = p("option");
		$result = $this->digg_model->set_digg($userid, $author, $dataid, $option);
		$msg = "添加成功";
		exit($msg);
	}
	
	//删除小站
	function delete_site()
	{
		$this->load->model("site_model", "sm");
		$siteid = p("siteid");
		$this->sm->delete($siteid);
		$msg = "删除成功";
		exit($msg);
	}
	
	//专题
	function add_special()
	{
		$this->load->model("special_model");
		//已参与检测
		$ischeckd = $this->special_model->check_special(p("dataid"), p("userid"));
		if($ischeckd) {
			echo 0;
			exit;
		}
		$data = array(
			"dataid" => p("dataid"),
			"userid" => p("userid"),
			"typeid" => p("typeid"),
		);
		$result = $this->special_model->add_special($data);
		echo $result;
		exit;
	}
	
	//社交活动
	function add_social()
	{
		$this->load->model("social_model");
		//已参与检测
		$ischeckd = $this->social_model->check_social(p("dataid"), p("userid"));
		if($ischeckd) {
			echo 0;
			exit;
		}
		$data = array(
			"dataid" => p("dataid"),
			"userid" => p("userid"),
			"typeid" => p("typeid"),
		);
		$result = $this->social_model->add_social($data);
		echo $result;
		exit;
	}
	
	//删除选项应用
	function del_plugin()
	{
		$this->load->model("plugins_model", "pm");
		$plid = p("plid");
		$result = $this->pm->del_plugin($plid);
		exit;
	}
	
	//编辑选项应用
	function edit_plugin()
	{
		$this->load->model("plugins_model", "pm");
		$plid = p("plid");
		$plname = p("plname");
		$public = p("ispublic");
		$result = $this->pm->edit_plugin($plid, $plname, $public);
		echo $result;
		exit;
	}
	
	//添加选项应用
	function add_plugins()
	{
		$this->load->model("plugins_model", "pm");
		$opid = p("opid");
		$plname = p("plname");
		$moduleid = p("moduleid");
		$result = $this->pm->add_plugins($opid, $plname, $moduleid);
		echo $result;
		exit;
	}
	
	//删除选项
	function del_option()
	{
		$this->load->model("options_model", "om");
		$optid = p("optid");
		$result = $this->om->del_option($optid);
		exit;
	}
	
	//编辑选项
	function edit_option()
	{
		$this->load->model("options_model", "om");
		$optid = p("optid");
		$opname = p("opname");
		$result = $this->om->edit_option($optid, $opname);
		exit;
	}
	
	//小站添加选项
	function add_options()
	{
		$this->load->model("options_model", "om");
		$siteid = p("siteid");
		$opname = p("opname");
		$result = $this->om->add_options($siteid, $opname);
		echo $result;
		exit;		
	}
	
	//添加关注
	function set_fans()
	{
		
		$this->load->model("sitestatis_model", "ssm");
		$siteid = p("siteid");
		$userid = p("userid");
		$siteuserid = p("siteuserid");
		$result = $this->ssm->funs_record($siteid, $userid);
		$data = array(
			"result" => $result,
			"button" => "<input type='button'  class='cancerKey' value='取消关注' onclick='aj_unset_fans(".$siteid.",". $userid.");'>",
		);
		$msg = json_encode($data);//json格式
		
		//发送通知
		$this->load->model("message_model");
		$this->load->model("accounts_model", "am");

		$this->load->model("site_model", "sm");
		$user = $this->am->get($userid);
		$site = $this->sm->get_site($siteid);
		$title = '<a href="/home/profile/'.$userid.'">'.$user['username'].'</a>关注了<a href="'.c('site_url').$site['domain'].'">'.$site['name'].'</a>';
		$this->message_model->send_notice($siteuserid, $title);
		exit($msg);
	}
	
	//取消关注
	function unset_fans()
	{
		$this->load->model("sitestatis_model", "ssm");
		$this->load->model("site_model", "sm");
		$siteid = p("siteid");
		$userid = p("userid");
		//验证是否为自己
		$site = $this->sm->get_site($siteid);
		if($site["userid"] == $userid) exit("");
		
		//执行
		$result = $this->ssm->funs_unrecord($siteid, $userid);
		$data = array(
			"result" => $result,
			"button" => "<input type='button' class='cancerKey' value='关注' onclick='aj_set_fans(".$siteid.",". $userid.");'>",
		);
		$msg = json_encode($data);//json格式
		exit($msg);
	}
	
	//修改连接颜色
	function change_linkcolor()
	{
		$this->load->model("site_model", "sm");
		$siteid = p("siteid");
		$color = p("color");
		$data = array("color"=>$color);
		$result = $this->sm->update_siteinfo($siteid, $data);
		if($result) {
			$msg = "保存成功";
		} else {
			$msg = "保存失败";
		}
		exit($msg);
	}
	
	//联动选择分类
	function change_class()
	{
		$this->load->model("classes_model", "cm");
		$parentid = p("parentid");
		if($parentid == 0) exit("");//不获取顶级分类
		$classes = $this->cm->get_subclass($parentid);
		$msg = "";
		if($classes) {
			$msg .= '<option value="">请选择</option>';
			foreach($classes as $class){
				$msg .= '<option value="'.$class["id"].'">'.$class["name"].'</option>';
			}	
		} 
		exit($msg);
	}
	
	//保存小站风格修改
	function save_siteskin()
	{
		$this->load->model("site_model", "sm");
		$siteid = p("siteid");
		$skinid = p("skinid");
		$data = array(
			"background" => "",
			"color" => "",
			"skin" => $skinid,
		);
		//修改描述信息
		$result = $this->sm->update_siteinfo($siteid, $data);
		if($result) {
			$msg = "保存成功";
		} else {
			$msg = "保存失败";
		}
		exit($msg);
	}
	
	//保存用户信息修改
	function save_userinfo()
	{
		$this->load->library("session");
		$this->load->model("accounts_model", "am");
		$this->load->model("tag_model");
		$userid = p("userid");
		$username = p("username",true,true);
		$exsits_username = $this->am->aj_exsits_username($userid, $username);
		if($exsits_username) {
			$msg = "用户名已存在，保存失败";
			exit($msg);
		}
		$data = array(
			'username' => p("username"),
			'gender' => p("gender"),
			'birthday' => strtotime(p("birthday")),
			'description' => p("description",true,true),
			'province' => p("province"),
			'city' => p("city"),
			'county' => p("county"),
		);
		$result = $this->am->update_userdata($userid, $data);
		
		//更新session中用户名
		$sessdata = array(
			"account_username" => p("username"),
		);
		$this->session->set_userdata($sessdata);
		
		//修改标签信息
		$tag = p("tag");
		$this->tag_model->update_user($userid, $tag);
		
		if($result) {
			$msg = "保存成功";
		} else {
			$msg = "保存失败";
		}
		exit($msg);
	}
	
	//保存用户密码修改
	function save_userpassword()
	{
		$this->load->model("accounts_model", "am");
		$userid = p("userid");
		
		//检测密码
		$check = $this->am->check_password($userid, p("oldpassword"));
		if(!$check) exit("旧密码输入错误");
		
		$data = array(
			'password' => md5(p("newpassword")),
		);
		$result = $this->am->update_userdata($userid, $data);

		if($result) {
			$msg = "保存成功";
		} else {
			$msg = "保存失败";
		}
		exit($msg);
	}
	
	//保存小站描述修改
	function save_sitedesc()
	{
		$this->load->model("site_model", "sm");
		$this->load->model("sitestatis_model", "ssm");
		$this->load->model("tag_model");
		$this->load->model("forum_model");
		$siteid = p("siteid");
		$desc = p("desc",true,true);
		$classid = p("classid");
		$data = array("description"=>$desc);
		
		//修改描述信息
		$result = $this->sm->update_siteinfo($siteid, $data);
		$data = array("classid"=>$classid);
		$result = $this->sm->update_site($siteid, $data);
		//修改话题分类
		$this->forum_model->update_class($siteid, $classid);
		//修改统计分类
		$this->ssm->update_class($siteid, $classid);
		
		//修改标签信息
		$tag = p("tag");
		$this->tag_model->update_site($siteid, $tag);
		
		if($result) {
			$msg = "保存成功";
		} else {
			$msg = "保存失败";
		}
		exit($msg);
	}
	
	//保存小站LOGO修改
	function save_sitelogo()
	{
		$siteid = p("siteid");
		//加载类库
		$this->load->library("jqphoto");
		$post = array(
			"x1" => p("x1"),
			"x2" => p("x2"),
			"y1" => p("y1"),
			"y2" => p("y2"),
			"w" => p("w"),
			"h" => p("h"),
		);
		$logo = $this->jqphoto->upload_thumb($post);
		$msg = "";
		if($logo) {
			$this->load->model("site_model", "sm");
			$data = array("logo"=>$logo);
			$result = $this->sm->update_siteinfo($siteid, $data);
			if($result) {
				$msg = "保存成功";
			} 
		}
		exit($msg);
	}
	
	//保存小站隐藏添加选项提示
	function save_sitetips()
	{
		$siteid = p("siteid");	
		$this->load->model("site_model", "sm");
		$data = array("hidetips"=>1);
		$result = $this->sm->update_site($siteid, $data);
		exit(1);
	}
	
	//保存个人头像修改
	function save_userphoto()
	{
		$userid = p("userid");
		//加载类库
		$this->load->library("jqphoto");
		$this->jqphoto->thumb_width = "180";
		$this->jqphoto->thumb_height = "180";
		$post = array(
			"x1" => p("x1"),
			"x2" => p("x2"),
			"y1" => p("y1"),
			"y2" => p("y2"),
			"w" => p("w"),
			"h" => p("h"),
		);
		$photo = $this->jqphoto->upload_thumb($post);
		$msg = "";
		if($photo) {
			$this->load->model("accounts_model", "am");
			$data = array("photo"=>$photo);
			$result = $this->am->update_userdata($userid, $data);
			if($result) {
				$msg = "保存成功";
			} 
		}
		exit($msg);
	}
	
	//验证登录
	function check_login()
	{
		$this->load->library("session");
		$this->load->model("accounts_model", "am");
		$logindata = array(
		"email" => p("email"),
		"password" => p("password"),
		);
		$result = $this->am->login($logindata);
		if (!$result) {
			$msg = "0";
		} else {
			$msg = "1";
		}
		exit($msg);
	}
	
	//检测注册
	function check_register()
	{
		$type = p("type");
		$value = p("value");
		$msg = "";
		switch($type) {
			case "email"://邮件
				if($this->__exsits_email($value)){
					$msg = "已经被注册";
				}
			break;
			case "username"://用户名
				if($this->__exsits_username($value)){
					$msg = "已经被注册";
				}
			break;
			default:
				$msg = "错误请求";
			break;
		}
		exit($msg);
	}
	
	//检测开好站
	function check_opening()
	{
		$type = p("type");
		$value = p("value");
		$msg = "";
		switch($type) {
			case "name"://好站名称
				if($this->__exsits_name($value)){
					$msg = "名称已经被申请";
				}
			break;
			case "domain"://好站域名
				if($this->__exsits_domain($value)){
					$msg = "域名已经被申请";
				}
			break;
			default:
				$msg = "错误请求";
			break;
		}
		exit($msg);
	}
	
	//检测邮件地址是否存在
	function __exsits_email($email)
	{
		$this->load->model("accounts_model", "am");
		return $this->am->exsits_email($email);
	}
	
	//检测用户名是否存在
	function __exsits_username($username)
	{
		$this->load->model("accounts_model", "am");
		return $this->am->exsits_username($username);
	}
	
	//检测好站名称是否存在
	function __exsits_name($name)
	{
		$this->load->model("site_model", "sm");
		return $this->sm->exsits_name($name);
	}
	
	//检测好站域名是否存在
	function __exsits_domain($domain)
	{
		$this->load->model("site_model", "sm");
		return $this->sm->exsits_domain($domain);
	}
}

/* End of file */